define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        create: function () {
            $(document).on("click", ".goods_name", function (e) {
                var xh = (e.target.dataset.xh) * 1
                var url = 'basic/goods/select';
                var msg = '商品列表';
                var width = '1200px';
                var height = '800px';
                var area = [$(window).width() > 800 ? (width ? width : '800px') : '95%', $(window).height() > 600 ? (height ? height : '600px') : '95%'];
                var options = {
                    shadeClose: false,
                    shade: [0.3, '#393D49'],
                    area: area,
                    callback: function (value) {

                        //这个value就是传上来的值
                        $('.goods_id' + xh).val(value.id)
                        $('.goods_name' + xh).val(value.name)
                        $('.goods_number' + xh).val(value.number)
                        if (value.jh_price) {
                            fz(xh, 1, value.jh_price.price)
                        } else {
                            fz(xh, 1, 2)
                        }

                        var num = xh += 1;
                        var html = getHtml(num);
                        if ($('.goods' + num).length <= 0) {
                            $('#goods').append(html)
                        }
                        getSumQty();
                        getSumAmount();
                    }
                };

                Fast.api.open(url, msg, options);

            })

            $(document).on("click", ".goods_del", function (e) {
                var xh = (e.target.dataset.xh) * 1
                if (xh != 1) {
                    $('.goods' + xh).remove();
                }
            })


            $(document).on("keyup", ".goods_qty", function (e) {
                var xh = $(this).attr('data-xh')
                if ($('.goods_id' + xh).val()) {
                    var qty = $(this).val();
                    var price = $('.goods_price' + xh).val();
                    qty = parseFloat(qty)
                    price = parseFloat(price)
                    $('.goods_amount' + xh).val((qty * price).toFixed(2));
                    getSumQty();
                    getSumAmount();
                } else {
                    $(this).val('');
                }
            });

            $(document).on("keyup", ".goods_price", function (e) {
                var xh = $(this).attr('data-xh')
                if ($('.goods_id' + xh).val()) {
                    var price = $(this).val();
                    var qty = $('.goods_qty' + xh).val();
                    qty = parseFloat(qty)
                    price = parseFloat(price)
                    $('.goods_amount' + xh).val((qty * price).toFixed(2));
                    getSumQty();
                    getSumAmount();
                } else {
                    $(this).val('');
                }
            });

            // $(".goods_amount").bind("input propertychange", function (event) {
            $(document).on("keyup", ".goods_amount", function (e) {
                var xh = $(this).attr('data-xh')
                var amount = $(this).val();
                if ($('.goods_id' + xh).val() && amount) {

                    var qty = $('.goods_qty' + xh).val();
                    qty = parseFloat(qty)
                    amount = parseFloat(amount)
                    $('.goods_price' + xh).val((amount / qty).toFixed(2));
                    getSumQty();
                    getSumAmount();
                } else {
                    $(this).val('');
                }
            });

            Form.api.bindevent($("form[role=form]"), function () {
            }, function () {
            }, function (success, error) {
                layer.confirm('请仔细核对单据！避免出错。', {
                    btn: ['确定', '取消'] //按钮
                }, function (index) {
                    Form.api.submit($("form[role=form]"), function (data, ret) {
                        if(ret.code == 1){
                            setTimeout(function(){
                                location.reload()
                            },1000)
                        }
                    });
                    Layer.close(index);
                }, function () {
                });
                return false;

            });
        },
        other_warehousing: function () {
            $(document).on("click", ".goods_name", function (e) {
                var xh = (e.target.dataset.xh) * 1
                var url = 'basic/goods/select';
                var msg = '商品列表';
                var width = '1200px';
                var height = '800px';
                var area = [$(window).width() > 800 ? (width ? width : '800px') : '95%', $(window).height() > 600 ? (height ? height : '600px') : '95%'];
                var options = {
                    shadeClose: false,
                    shade: [0.3, '#393D49'],
                    area: area,
                    callback: function (value) {

                        //这个value就是传上来的值
                        $('.goods_id' + xh).val(value.id)
                        $('.goods_name' + xh).val(value.name)
                        $('.goods_number' + xh).val(value.number)
                        if (value.jh_price) {
                            fz(xh, 1, value.jh_price.price)
                        } else {
                            fz(xh, 1, 2)
                        }

                        var num = xh += 1;
                        var html = getHtml(num);
                        if ($('.goods' + num).length <= 0) {
                            $('#goods').append(html)
                        }
                        getSumQty();
                        getSumAmount();
                    }
                };

                Fast.api.open(url, msg, options);

            })

            $(document).on("click", ".goods_del", function (e) {
                var xh = (e.target.dataset.xh) * 1
                if (xh != 1) {
                    $('.goods' + xh).remove();
                }
            })


            $(document).on("keyup", ".goods_qty", function (e) {
                var xh = $(this).attr('data-xh')
                if ($('.goods_id' + xh).val()) {
                    var qty = $(this).val();
                    var price = $('.goods_price' + xh).val();
                    qty = parseFloat(qty)
                    price = parseFloat(price)
                    $('.goods_amount' + xh).val((qty * price).toFixed(2));
                    getSumQty();
                    getSumAmount();
                } else {
                    $(this).val('');
                }
            });

            $(document).on("keyup", ".goods_price", function (e) {
                var xh = $(this).attr('data-xh')
                if ($('.goods_id' + xh).val()) {
                    var price = $(this).val();
                    var qty = $('.goods_qty' + xh).val();
                    qty = parseFloat(qty)
                    price = parseFloat(price)
                    $('.goods_amount' + xh).val((qty * price).toFixed(2));
                    getSumQty();
                    getSumAmount();
                } else {
                    $(this).val('');
                }
            });

            // $(".goods_amount").bind("input propertychange", function (event) {
            $(document).on("keyup", ".goods_amount", function (e) {
                var xh = $(this).attr('data-xh')
                var amount = $(this).val();
                if ($('.goods_id' + xh).val() && amount) {

                    var qty = $('.goods_qty' + xh).val();
                    qty = parseFloat(qty)
                    amount = parseFloat(amount)
                    $('.goods_price' + xh).val((amount / qty).toFixed(2));
                    getSumQty();
                    getSumAmount();
                } else {
                    $(this).val('');
                }
            });

            Form.api.bindevent($("form[role=form]"), function () {
            }, function () {
            }, function (success, error) {
                layer.confirm('请仔细核对单据！避免出错。', {
                    btn: ['确定', '取消'] //按钮
                }, function (index) {
                    Form.api.submit($("form[role=form]"), function (data, ret) {
                        if(ret.code == 1){
                            setTimeout(function(){
                                location.reload()
                            },1000)
                        }
                    });
                    Layer.close(index);
                }, function () {
                });
                return false;

            });
        },
        other_outbound: function () {
            $(document).on("click", ".goods_name", function (e) {
                var xh = (e.target.dataset.xh) * 1
                var url = 'basic/goods/select';
                var msg = '商品列表';
                var width = '1200px';
                var height = '800px';
                var area = [$(window).width() > 800 ? (width ? width : '800px') : '95%', $(window).height() > 600 ? (height ? height : '600px') : '95%'];
                var options = {
                    shadeClose: false,
                    shade: [0.3, '#393D49'],
                    area: area,
                    callback: function (value) {

                        //这个value就是传上来的值
                        $('.goods_id' + xh).val(value.id)
                        $('.goods_name' + xh).val(value.name)
                        $('.goods_number' + xh).val(value.number)
                        if (value.jh_price) {
                            fz(xh, 1, value.jh_price.price)
                        } else {
                            fz(xh, 1, 2)
                        }

                        var num = xh += 1;
                        var html = getHtml(num);
                        if ($('.goods' + num).length <= 0) {
                            $('#goods').append(html)
                        }
                        getSumQty();
                        getSumAmount();
                    }
                };

                Fast.api.open(url, msg, options);

            })

            $(document).on("click", ".goods_del", function (e) {
                var xh = (e.target.dataset.xh) * 1
                if (xh != 1) {
                    $('.goods' + xh).remove();
                }
            })


            $(document).on("keyup", ".goods_qty", function (e) {
                var xh = $(this).attr('data-xh')
                if ($('.goods_id' + xh).val()) {
                    var qty = $(this).val();
                    var price = $('.goods_price' + xh).val();
                    qty = parseFloat(qty)
                    price = parseFloat(price)
                    $('.goods_amount' + xh).val((qty * price).toFixed(2));
                    getSumQty();
                    getSumAmount();
                } else {
                    $(this).val('');
                }
            });

            $(document).on("keyup", ".goods_price", function (e) {
                var xh = $(this).attr('data-xh')
                if ($('.goods_id' + xh).val()) {
                    var price = $(this).val();
                    var qty = $('.goods_qty' + xh).val();
                    qty = parseFloat(qty)
                    price = parseFloat(price)
                    $('.goods_amount' + xh).val((qty * price).toFixed(2));
                    getSumQty();
                    getSumAmount();
                } else {
                    $(this).val('');
                }
            });

            // $(".goods_amount").bind("input propertychange", function (event) {
            $(document).on("keyup", ".goods_amount", function (e) {
                var xh = $(this).attr('data-xh')
                var amount = $(this).val();
                if ($('.goods_id' + xh).val() && amount) {

                    var qty = $('.goods_qty' + xh).val();
                    qty = parseFloat(qty)
                    amount = parseFloat(amount)
                    $('.goods_price' + xh).val((amount / qty).toFixed(2));
                    getSumQty();
                    getSumAmount();
                } else {
                    $(this).val('');
                }
            });

            Form.api.bindevent($("form[role=form]"), function () {
            }, function () {
            }, function (success, error) {
                layer.confirm('请仔细核对单据！避免出错。', {
                    btn: ['确定', '取消'] //按钮
                }, function (index) {
                    Form.api.submit($("form[role=form]"), function (data, ret) {
                        if(ret.code == 1){
                            setTimeout(function(){
                                location.reload()
                            },1000)
                        }
                    });
                    Layer.close(index);
                }, function () {
                });
                return false;

            });
        },
    };

    function getSumQty() {
        var inputs = $('.goods_qty');
        var sum_qty = 0;
        inputs.each(function () {
            sum_qty = sum_qty + $(this).val() * 1;
        });
        $('#sum_qty').text(sum_qty);
    }

    function getSumAmount() {
        var inputs = $('.goods_amount');
        var sum_amount = 0;
        inputs.each(function () {
            sum_amount = sum_amount + $(this).val() * 1;
        });
        $('#sum_amount').text(sum_amount);
    }

    function getSumAssets() {
        var inputs = $('.assets_price');
        var sum_amount = 0;
        inputs.each(function () {
            sum_amount = sum_amount + $(this).val() * 1;
        });
        $('input[name=amount]').val(sum_amount);
    }

    function fz(xh, qty, price) {
        $('.goods_qty' + xh).val(qty);
        $('.goods_price' + xh).val(price);
        qty = parseFloat(qty)
        price = parseFloat(price)
        $('.goods_amount' + xh).val((qty * price).toFixed(2));
    }

    function getHtml(num) {
        var html = '        <div class="form-group col-xs-12 goods' + num + '" style="margin-left: 5%">\n' +
            '            <label class="control-label col-xs-1 col-sm-1">' + num + '</label>\n' +
            '            <div class="col-xs-1 col-sm-1 cy-div">\n' +
            '                <input data-rule=""\n' +
            '                       class="form-control goods_number  goods_number' + num + '" data-xh="' + num + '"\n' +
            '                       name="goods_number[]" type="text" value="" readonly data-field="number" placeholder="商品编号">' +
            '  <input data-rule=""\n' +
            '                           class="form-control goods_id goods_id' + num + '" readonly data-xh="1"\n' +
            '                           name="goods_id[]" type="hidden" value="" data-field="number" placeholder="商品id">\n' +
            '            </div>\n' +
            '            <div class="col-xs-3 col-sm-3 cy-div">\n' +
            '                <input data-rule=""\n' +
            '                       class="form-control goods_name goods_name' + num + '" readonly name="goods_name[]" data-xh="' + num + '" type="text"\n' +
            '                         value="" placeholder="点击选择商品">\n' +
            '            </div>\n' +
            '            <div class="col-xs-1 col-sm-1 cy-div">\n' +
            '                <input data-rule=""\n' +
            '                       class="form-control goods_qty goods_qty' + num + '" name="goods_qty[]" type="text" value="" data-xh="' + num + '"\n' +
            '                       placeholder="数量">\n' +
            '            </div>\n' +
            '            <div class="col-xs-1 col-sm-1 cy-div">\n' +
            '                <input data-rule=""\n' +
            '                       class="form-control goods_price goods_price' + num + '" name="goods_price[]" type="text" value="" data-xh="' + num + '"\n' +
            '                       placeholder="单价">\n' +
            '            </div>\n' +
            '            <div class="col-xs-1 col-sm-1 cy-div">\n' +
            '                <input data-rule=""\n' +
            '                       class="form-control goods_amount goods_amount' + num + '" name="goods_amount[]" type="text" value="" data-xh="' + num + '"\n' +
            '                       placeholder="金额">\n' +
            '            </div>\n' +
            '            <div class="col-xs-2 col-sm-2 cy-div">\n' +
            '                <input data-rule=""\n' +
            '                       class="form-control goods_remarks goods_remarks' + num + '" name="goods_remarks[]" type="text" value="" data-xh="' + num + '"\n' +
            '                       placeholder="备注">\n' +
            '            </div>\n' +
            '   <div data-xh="' + num + '" class="col-xs-1 col-sm-1 cy-div goods_del" style="line-height: 35px;padding-left: 10px; cursor: pointer;">\n' +
            '                    删除\n' +
            '                </div>' +
            '        </div>'

        return html
    }
    function getHtmlAsstes(num) {
        var html = '        <div class="form-group col-xs-12 assets' + num + '" style="margin-left: 5%">\n' +
            '            <label class="control-label col-xs-1 col-sm-1">' + num + '</label>\n' +
            '            <div class="col-xs-1 col-sm-1 cy-div">\n' +
            '                <input data-rule=""\n' +
            '                       class="form-control assets_number  assets_number' + num + '" data-xh="' + num + '"\n' +
            '                       name="assets_number[]" type="text" value="" readonly data-field="number" placeholder="账户编号">' +
            '  <input data-rule=""\n' +
            '                           class="form-control assets_id assets_id' + num + '" readonly data-xh="1"\n' +
            '                           name="assets_id[]" type="hidden" value="" data-field="number" placeholder="账户idid">\n' +
            '            </div>\n' +
            '            <div class="col-xs-3 col-sm-3 cy-div">\n' +
            '                <input data-rule=""\n' +
            '                       class="form-control assets_name assets_name' + num + '" readonly name="assets_name[]" data-xh="' + num + '" type="text"\n' +
            '                         value="" placeholder="点击选择账户">\n' +
            '            </div>\n' +
            '            <div class="col-xs-1 col-sm-1 cy-div">\n' +
            '                <input data-rule=""\n' +
            '                       class="form-control assets_price assets_price' + num + '" name="assets_price[]" type="text" value="" data-xh="' + num + '"\n' +
            '                       placeholder="金额">\n' +
            '            </div>\n' +
            '            <div class="col-xs-2 col-sm-2 cy-div">\n' +
            '                <input data-rule=""\n' +
            '                       class="form-control assets_remarks assets_remarks' + num + '" name="assets_remarks[]" type="text" value="" data-xh="' + num + '"\n' +
            '                       placeholder="备注">\n' +
            '            </div>\n' +
            '   <div data-xh="' + num + '" class="col-xs-1 col-sm-1 cy-div goods_del" style="line-height: 35px;padding-left: 10px; cursor: pointer;">\n' +
            '                    删除\n' +
            '                </div>' +
            '        </div>'

        return html
    }
    return Controller;
});
